package com.fare.taluo.web.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.income.FreightPublishApplication;
import com.fare.taluo.vo.core.FreightPublishVO;

@Controller("freightPublishAction")
@Scope("prototype")
public class FreightPublishAction extends BaseFdcAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = 5786843553290814843L;
	@Resource(name="freightPublishApplication")
    private FreightPublishApplication freightPublishApplication ;
    private String flag;
    private FreightPublishVO vo;
    @Resource(name="portApplication")
    private PortApplication portApplication;
    
	
	public String dealSomething() throws UnsupportedEncodingException{
		
		if("save".equals(flag)||"update".equals(flag)){
			String fromCityName = new String(vo.getFromCityName().getBytes("ISO-8859-1"),"UTF-8");
			String toCityName = new String(vo.getToCityName().getBytes("ISO-8859-1"),"UTF-8");
			String fromCity = portApplication.findPortEntityByCityName(fromCityName).getPortCode();
			String toCity = portApplication.findPortEntityByCityName(toCityName).getPortCode();
			vo.setFromCityName(fromCityName);
			vo.setToCityName(toCityName);
			vo.setFromCity(fromCity);
			vo.setToCity(toCity);
			freightPublishApplication.saveOrUpdate(vo);
		}else if("delete".equals(flag)){
			freightPublishApplication.remove(vo.getId());
		}
		returnPageURL = "/system/maintain/freightPublish.jsp";
		return "dispatcher";
	}
	
	public void getData() throws JsonGenerationException, JsonMappingException, IOException{
		List<FreightPublishVO> list = freightPublishApplication.findAll();	
		StringBuffer resultBuffer = new StringBuffer();
		String jsonStr = com.fare.taluo.util.JsonUtil.toJson(list);
		resultBuffer.append("{\"Total\":"+list.size()+","+ "\"Rows\":"+jsonStr+"}");	
		/*resultBuffer.append("{'Total':"+list.size()+","+"'Rows':[");
		for (FreightPublishVO freightPublishVO : list) {
			resultBuffer.append("{'id':'"+freightPublishVO.getId()+"',");
			resultBuffer.append("'fromCityName':'"+freightPublishVO.getFromCityName()+"',");
			resultBuffer.append("'toCityName':'"+freightPublishVO.getToCityName()+"',");
			resultBuffer.append("'tourist':'"+freightPublishVO.getTourist()+"',");
			resultBuffer.append("'official':'"+freightPublishVO.getOfficial()+"',");
			resultBuffer.append("'bussiness':'"+freightPublishVO.getBussiness()+"',");
			resultBuffer.append("'standardPrice':'"+freightPublishVO.getStandardPrice()+"'");	
			resultBuffer.append("},");
		}
		if(resultBuffer.toString().endsWith(","))
			resultBuffer.deleteCharAt(resultBuffer.lastIndexOf(","));
		resultBuffer.append("]}");*/
		String json = resultBuffer.toString();
		super.toJSONOut(json);		
	}
	
	/**
	 * 查询
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public void findByCity() throws JsonGenerationException, JsonMappingException, IOException{
		String fromCityName= request.getParameter("fromCtity");
		String toCityName= request.getParameter("toCity");
		String fromCity=null;
		String toCity=null;
		if(!"".equals(fromCityName)){
			fromCity = portApplication.findPortEntityByCityName(fromCityName).getPortCode();	
		}
		if(!"".equals(toCityName)){
			 toCity = portApplication.findPortEntityByCityName(toCityName).getPortCode();
		}
		List<FreightPublishVO> list = new ArrayList<FreightPublishVO>();
		FreightPublishVO vo =freightPublishApplication.findByLine(fromCity, toCity);
		list.add(vo);
		StringBuffer resultBuffer = new StringBuffer();
		String jsonStr = com.fare.taluo.util.JsonUtil.toJson(list);
		resultBuffer.append("{\"Total\":"+list.size()+","+ "\"Rows\":"+jsonStr+"}");
		String json = resultBuffer.toString();
		super.toJSONOut(json);	
	}
	
	public String toView(){
		returnPageURL = "/system/maintain/freightPublish.jsp";
		return "dispatcher";
	}
 
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public FreightPublishVO getVo() {
		return vo;
	}

	public void setVo(FreightPublishVO vo) {
		this.vo = vo;
	}
	
	
	

}
